﻿using System;
using System.Data.Linq;
using System.Linq;
using System.Security.Cryptography;
using Domain;
using NUnit.Framework;

namespace IntegrationTests
{
    [TestFixture]
    public class RepositoryTest
    {
        private RepositoryDataContext repository;
        private string username;
        private string password;
        private string title;

        [SetUp]
        public void SetUp()
        {
            repository = new RepositoryDataContext();
            username = "test";
            password = "password";
            title = "testAccount";
            repository.Profiles.InsertOnSubmit(new Profile(){Title=title , Username = username,Password = password});
            repository.SubmitChanges(ConflictMode.FailOnFirstConflict);
        }

        [Test]
        public void ShouldReturnUserProfileFromDb()
        {
            Profile profile = repository.GetProfile(username,password);
            Assert.IsNotNull(profile);
        }

        [Test]
        public void ShouldGetTransactionsFromAccount()
        {
            Profile profile = repository.GetProfile(username,password);
            var salaryAccount = new Account()
                              {
                                  Title = "Salary",
                                  IsInFlowAccount = true,
                                  IsOutFlowAccount = true,
                                  Profile = profile
                              };

             var shoppingAccount = new Account()
                              {
                                  Title = "Shopping",
                                  IsInFlowAccount = true,
                                  IsOutFlowAccount = true,
                                  Profile = profile
                              };
            repository.Accounts.InsertOnSubmit(salaryAccount);
            repository.Accounts.InsertOnSubmit(shoppingAccount);
            var transaction = new Transaction()
                                  {
                                      Date = DateTime.Now,
                                      Amount = 2400,
                                      Description = "Test Transaction",
                                      FromAccount = salaryAccount,
                                      ToAccount = shoppingAccount,
                                      Profile = profile
                                  };
            repository.Transactions.InsertOnSubmit(transaction);
            repository.SubmitChanges();

            Assert.AreEqual(1,salaryAccount.Transactions.Count);

        }

        [TearDown]
        public void TearDown()
        {
            repository.Profiles.DeleteAllOnSubmit(repository.Profiles.Where(x => x.Title == title));
            repository.SubmitChanges();
        }
    }
}
